package com.chesnutconsulting.otbasket.mail;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.inject.Inject;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Component
public class Emailer {
	final JavaMailSender mailSender;

	final VelocityEngine velocityEngine;

	@Inject
	public Emailer(JavaMailSender mailSender, VelocityEngine velocityEngine) {
		this.mailSender = mailSender;
		this.velocityEngine = velocityEngine;
	}

	// <<<<<<< HEAD
	public void emailBasket(final Iterable<ImageInfoAtom> imageInfoAtoms,
			final String subject, final String body,
			final List<String> toAddresses, final String from, final boolean sendAsHtml, final String fromFormElem) {
		System.out.println("Sending atoms [" + imageInfoAtoms + "]");
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {

				System.out.println("Preparing message");

				MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
						true);
				for (String to : toAddresses) {
					message.addTo(to);
				}
				message.setFrom(from);
				message.setSubject(subject);
				Map<String, Object> model = new HashMap<String, Object>();
				model.put("imageInfoAtoms", imageInfoAtoms);
				model.put("body", body);
				model.put("fromFormElem", fromFormElem);

				System.out.println("Merging template");
				
				String template = (sendAsHtml)?"basket-email.vm":"basket-email-text.vm";
				String text = VelocityEngineUtils.mergeTemplateIntoString(
						velocityEngine, template, model);

				System.out.println("Setting email text [" + text + "]");

				message.setText(text, true);
				for (ImageInfoAtom atom : imageInfoAtoms) {
					message.addInline(atom.getContentId(),
							atom.getWatermarkedImageFile());
				}

				/*
				 * ======= public void emailBasket(final String to, final String
				 * subject, final String additionalMessage, final
				 * Iterable<ImageInfoAtom> imageInfoAtoms) {
				 * MimeMessagePreparator preparator = new
				 * MimeMessagePreparator() { public void prepare(MimeMessage
				 * mimeMessage) throws Exception { MimeMessageHelper message =
				 * new MimeMessageHelper(mimeMessage, true); message.setTo(to);
				 * message.setSubject(subject); Map<String, Object> model = new
				 * HashMap<String, Object>(); model.put("imageInfoAtoms",
				 * imageInfoAtoms); model.put("additionalMessage",
				 * additionalMessage); >>>>>>>
				 * 31a54b969397281ed894a1de0e1e3dc0b1f035db String text =
				 * VelocityEngineUtils.mergeTemplateIntoString( velocityEngine,
				 * "basket-email.vm", model);
				 * 
				 * System.out.println("Setting email text [" + text + "]");
				 * 
				 * message.setText(text, true);
				 * 
				 * for(ImageInfoAtom atom : imageInfoAtoms) {
				 * message.addInline(atom.getContentId(),
				 * atom.getWatermarkedImageFile()); } } };
				 * 
				 * 
				 * this.mailSender.send(preparator); }
				 */
			}
		};
		System.out.println("Just before send");
		this.mailSender.send(preparator);
	}
}
